from bs4 import BeautifulSoup
import bs4
import os
from opencc import OpenCC
import chardet  # 需要先安装：pip install chardet

# 初始化OpenCC对象
cc = OpenCC('t2s')

def detect_encoding(file_path):
    """自动检测文件编码"""
    with open(file_path, 'rb') as f:
        result = chardet.detect(f.read())
    return result['encoding'] or 'GB18030'

def html_to_txt(input_dir, output_dir):
    os.makedirs(output_dir, exist_ok=True)

    for filename in os.listdir(input_dir):
        if filename.endswith(".htm"):
            html_path = os.path.join(input_dir, filename)
            txt_filename = cc.convert(os.path.splitext(filename)[0]) + ".txt"
            txt_path = os.path.join(output_dir, txt_filename)

            try:
                # 自动检测编码并读取
                file_encoding = detect_encoding(html_path)
                with open(html_path, "r", encoding=file_encoding, errors='ignore') as f:
                    html_content = f.read()

                # 解析HTML内容
                soup = BeautifulSoup(html_content, 'lxml')
                
                # 查找分隔线逻辑保持不变
                separators = soup.find_all(text=lambda t: t and "--------------------------------------------------------------------------------" in t)
                if len(separators) >= 2:
                    # 提取两个分隔线之间的内容
                    start = separators[0].find_parent().text
                    end = separators[1].find_parent().text
                    text = soup.get_text().split(start)[1].split(end)[0]
                else:
                    text = "未找到两个分隔线"

                text = cc.convert(text)
                
                with open(txt_path, "w", encoding="utf-8") as f:
                    f.write(text)
                print(f"转换成功：{filename} -> {txt_filename}")

            except UnicodeDecodeError:
                print(f"编码检测失败，尝试备选方案：{filename}")
                with open(html_path, "r", encoding="GB18030", errors='ignore') as f:
                    html_content = f.read()
                # 重新执行解析逻辑...
                
            except Exception as e:
                print(f"处理文件 {filename} 时出错：{str(e)}")

if __name__ == "__main__":
    # 使用原始字符串避免转义问题
    html_to_txt(r"D:\daxue", r"D:\daxue")